package com.mostromas.ess.product.controller;

import com.mostromas.ess.entities.Supplier;
import com.mostromas.ess.product.req.SupplierREQ;
import com.mostromas.ess.product.service.ISupplierService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 供应商表-前端控制器
 */
@Api(value = "供应商管理接口",description = "供应商管理接口，提供供应商的增、删、改、查")
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    /**
     * 分页条件查询供应商列表
     * @param req
     * @return
     */
    @PreAuthorize(("hasAuthority('supplier:search')"))
    @ApiOperation("根据供应商名称、联系人、地址查询供应商列表接口")
    @PostMapping("/search") //请求路径： /product/supplier/search
    public Result search(@RequestBody SupplierREQ req){
        return supplierService.queryPage(req);
    }

    @PreAuthorize(("hasAuthority('supplier:search')"))
    @ApiOperation("查询供应商详情接口")
    @ApiImplicitParam(name = "id",value = "供应商ID",required = true)
    @GetMapping("/{id}") //请求路径：/product/supplier/{id}
    public Result view(@PathVariable("id") String id){
        Supplier supplier = supplierService.getById(id);
        return Result.ok(supplier);
    }

    @PreAuthorize(("hasAuthority('supplier:edit')"))
    @ApiOperation("修改供应商信息接口")
    @PutMapping //put方式调用接口：/product/supplier
    public Result update(@RequestBody Supplier supplier){
        supplierService.updateById(supplier);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('supplier:add')"))
    @ApiOperation("新增供应商信息接口")
    @PostMapping //post方式调用接口：/product/supplier
    public Result save(@RequestBody Supplier supplier){
        supplierService.save(supplier);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('supplier:delete')"))
    @ApiOperation("删除供应商信息接口")
    @ApiImplicitParam(name = "id",value = "供应商ID",required = true)
    @DeleteMapping("/{id}") //delete方式调用接口：/product/supplier/{id}
    public Result delete(@PathVariable("id") String id){
        supplierService.removeById(id);
        return Result.ok();
    }

}
